<template>
    <el-popover placement="bottom" width="180" v-model="visible" trigger="click">
        <div class="task-popover-header">
            <span class="task-popover-header-title">变更状态</span>
        </div>
        <div class="task-popover-item-choose">
            <div class="task-popover-line-choose">
                <i class="iconfont task-popover-icon-choose" :class="itemData.icon" :style="{color:itemData.color}"></i>
                <div class="task-popover-line-left-choose"></div>
            </div>
            <span class="task-popover-item-choose-type">{{ itemData.label }}</span>
            <span class="task-popover-item-choose-title">可变更为:</span>
        </div>
        <div class="task-popover-item" @click="chooseItem(item,index)" v-for="(item,index) in itemForm" v-if="item.label != itemData.label && item.taskType == 1">
            <div class="task-popover-line">
                <div :class="[index == itemForm.length-1 ?'task-popover-line-left-half':'task-popover-line-left']">
                </div>
                <i class="iconfont icon-ziyuan task-popover-line-icon"></i>
            </div>
            <i class="iconfont task-popover-icon" :style="{'color':item.type == true ? item.color : '#C0C4CC'}" :class="item.icon"></i>
            <span class="task-popover-item-item" :style="{'color':item.type == true? '#2e405e' : '#C0C4CC'}">{{
          item.label
        }}</span>
        </div>
        <i slot="reference" class="iconfont task-popover-botton-icon" :class="itemData.icon" :style="{color:itemData.color}"></i>
    </el-popover>
</template>

<script>
import { taskChange } from "@/api/task.js";

export default {
    name: "chooseStatus",
    data() {
        return {
            form: [
                {
                    value: "00",
                    label: "待办的",
                    type: false,
                    color: "rgb(250, 145, 31)",
                    icon: "icon-shuangyuanquan-copy",
                    taskType: 0,
                },
                {
                    value: "01",
                    label: "已接收",
                    type: false,
                    color: "rgb(112, 164, 255)",
                    icon: "icon-yiwancheng-copy",
                    taskType: 0,
                },
                {
                    value: "02",
                    label: "设计中",
                    type: false,
                    color: "rgb(86, 198, 255)",
                    icon: "icon-designer-copy",
                    taskType: 0,
                },
                {
                    value: "04",
                    label: "开发中",
                    type: false,
                    color: "rgb(69, 240, 210)",
                    icon: "icon-shuangyuan",
                    taskType: 0,
                },
                {
                    value: "05",
                    label: "修复中",
                    type: false,
                    color: "rgb(85, 187, 255)",
                    icon: "icon-shuangyuan",
                    taskType: 0,
                },
                {
                    value: "06",
                    label: "进行中",
                    type: false,
                    color: "rgb(85, 187, 255)",
                    icon: "icon-shuangyuan",
                    taskType: 0,
                },
                {
                    value: "07",
                    label: "已完成",
                    type: false,
                    color: "rgb(70, 216, 171)",
                    icon: "icon-yiwancheng-copy",
                    taskType: 0,
                },
                {
                    value: "08",
                    label: "已验收",
                    type: false,
                    color: "rgb(80, 196, 27)",
                    icon: "icon-xiaolian1-copy",
                    taskType: 0,
                },
                {
                    value: "11",
                    label: "中止",
                    type: false,
                    color: "rgb(245, 108, 108)",
                    icon: "icon-zhongzhi",
                    taskType: 0,
                },
                {
                    value: "09",
                    label: "已取消",
                    type: false,
                    color: "rgb(248, 120, 103)",
                    icon: "icon-yemian-1",
                    taskType: 0,
                },
            ],
            itemForm: [],
            itemData: null,
            visible: false,
            typeApp: [
                { value: "00", name: "需求" },
                { value: "01", name: "设计" },
                { value: "02", name: "开发" },
                { value: "04", name: "测试" },
                { value: "05", name: "缺陷" },
                { value: "06", name: "其他" },
            ],
            optionalIndex: -1,
            chooseIndex: -1,
        };
    },
    props: ["taskForm"],
    watch: {
        taskForm: {
            handler(val, oldval) {
                this.form.forEach(function (item, indedx) {
                    if (val.taskType == "00") {
                        if (
                            item.value == "00" ||
                            item.value == "01" ||
                            item.value == "06" ||
                            item.value == "11" ||
                            item.value == "07" ||
                            item.value == "08" ||
                            item.value == "09"
                        ) {
                            item.taskType = 1;
                        } else {
                            item.taskType = 0;
                        }
                    } else if (val.taskType == "02") {
                        if (
                            item.value == "00" ||
                            item.value == "01" ||
                            item.value == "04" ||
                            item.value == "11" ||
                            item.value == "07" ||
                            item.value == "08" ||
                            item.value == "09"
                        ) {
                            item.taskType = 1;
                        } else {
                            item.taskType = 0;
                        }
                    } else if (val.taskType == "01") {
                        if (
                            item.value == "00" ||
                            item.value == "01" ||
                            item.value == "02" ||
                            item.value == "11" ||
                            item.value == "07" ||
                            item.value == "08" ||
                            item.value == "09"
                        ) {
                            item.taskType = 1;
                        } else {
                            item.taskType = 0;
                        }
                    } else if (val.taskType == "04" || val.taskType == "05") {
                        if (
                            item.value == "00" ||
                            item.value == "01" ||
                            item.value == "05" ||
                            item.value == "11" ||
                            item.value == "07" ||
                            item.value == "08" ||
                            item.value == "09"
                        ) {
                            item.taskType = 1;
                        } else {
                            item.taskType = 0;
                        }
                    } else if (val.taskType == "06") {
                        if (
                            item.value == "00" ||
                            item.value == "01" ||
                            item.value == "06" ||
                            item.value == "11" ||
                            item.value == "07" ||
                            item.value == "08" ||
                            item.value == "09"
                        ) {
                            item.taskType = 1;
                        } else {
                            item.taskType = 0;
                        }
                    }
                });
                this.filterItem(val.status);
            },
            // 深度观察监听
            deep: true,
            immediate: true,
        },
    },
    methods: {
        chooseItem(item, index) {
            var _this = this;
            if (item.type == true) {
                _this
                    .$confirm("是否" + item.label + "?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                    .then(() => {
                        var data = {
                            taskId: _this.taskForm.taskId,
                            status: item.value,
                        };
                        taskChange(data).then((res) => {
                            _this.filterItem(item.value);
                        });
                    })
                    .catch(() => {});
            }
        },
        filterItem(data) {
            var _this = this;
            _this.itemForm = JSON.parse(JSON.stringify(_this.form));
            _this.itemForm.forEach(function (item, index) {
                if (item.value == data) {
                    _this.itemData = item;
                    _this.visible = false;
                    if (_this.chooseIndex > 0) {
                        _this.itemForm[_this.chooseIndex].taskType = 1;
                    }
                    _this.chooseIndex = index;
                    _this.itemForm[index].taskType = 2;
                    _this.optionalClick(index);
                }
            });
            _this.judgeClick();
        },
        optionalClick(val) {
            var _this = this;
            ++val;
            if (val >= _this.itemForm.length) {
                _this.optionalIndex = -1;
                return;
            } else {
                if (_this.itemForm[val].taskType == 1) {
                    if (
                        (_this.itemData.value != "00" ||
                            _this.itemData.value != "01") &&
                        _this.itemForm[val].value == "09"
                    ) {
                        console.log(_this.itemForm[val].label);
                        _this.optionalIndex = -1;
                        return;
                    }
                    _this.optionalIndex = val;
                    return;
                } else {
                    _this.optionalClick(val);
                }
            }
        },
        judgeClick() {
            var _this = this;
            _this.itemForm.forEach(function (item, index) {
                item.type = false;
                if (
                    _this.itemData.value == "11" &&
                    (item.value == "02" ||
                        item.value == "04" ||
                        item.value == "05" ||
                        item.value == "06")
                ) {
                    item.type = true;
                }
                if (
                    (_this.itemData.value == "02" ||
                        _this.itemData.value == "04" ||
                        _this.itemData.value == "05" ||
                        _this.itemData.value == "06") &&
                    item.value == "11"
                ) {
                    item.type = true;
                }
                if (
                    _this.itemData.value != "11" && _this.itemData.value != "08" &&
                    (index <= _this.optionalIndex ||
                        ((_this.itemData.value == "00" ||
                            _this.itemData.value == "01") &&
                            item.value == "09"))
                ) {
                    item.type = true;
                }
            });
        },
    },
};
</script>

<style scoped>
.task-popover-header {
    padding: 10px 16px;
    border-bottom: 1px solid #dadee3;
    background-color: #f5f6f8;
}

.task-popover-header-title {
    font-size: 12px;
    color: #2e405e;
    font-weight: 700;
}

.task-popover-item {
    position: relative;
    height: 40px;
    padding: 10px 16px 10px 40px;
    cursor: pointer;
    -webkit-touch-callout: none;
    background-color: white;
    display: flex;
    flex-direction: row;
    align-items: center;
}

.task-popover-item:hover {
    background-color: #f5f6f8;
}

.task-popover-item-item {
    /*color: #2e405e;*/
    font-size: 12px;
    line-height: 18px;
}

.task-popover-line {
    position: absolute;
    height: 40px;
    top: 0;
    left: 22px;
}

.task-popover-line-left {
    height: 40px;
    width: 1px;
    background-color: rgba(218, 222, 227, 0.9);
    position: absolute;
    left: 0;
    top: 0;
}

.task-popover-line-left-half {
    height: 10px;
    width: 1px;
    background-color: rgba(218, 222, 227, 0.9);
    position: absolute;
    left: 0;
    top: 0;
    border-radius: 12px;
}

.task-popover-line-icon {
    font-size: 18px;
    color: #dadee3;
    line-height: 30px;
    position: absolute;
    left: -4px;
}

.task-popover-icon {
    font-size: 16px;
    margin-right: 5px;
}

.task-popover-item-choose {
    position: relative;
    height: 40px;
    padding: 10px 16px 10px 40px;
    cursor: pointer;
    -webkit-touch-callout: none;
    background-color: white;
    display: flex;
    flex-direction: row;
    align-items: center;
}

.task-popover-item-choose-type {
    font-size: 14px;
    font-weight: bold;
    color: #333333;
}

.task-popover-item-choose-title {
    font-size: 12px;
    color: #909aaa;
    margin-left: 10px;
}

.task-popover-line-choose {
    height: 40px;
    position: absolute;
    left: 22px;
    top: 0;
    display: flex;
    align-items: center;
}

.task-popover-icon-choose {
    font-size: 16px;
    position: absolute;
    left: -8px;
}

.task-popover-line-left-choose {
    height: 12px;
    width: 1px;
    position: absolute;
    left: 0px;
    bottom: 0;
    background-color: rgba(218, 222, 227, 0.9);
}

.task-popover-botton-icon {
    font-size: 16px;
}

.rejected {
    color: rgb(236, 0, 25);
}

.completed {
    color: rgb(254, 188, 8);
}

.inProgress {
    color: rgb(74, 144, 226);
}

.toDo {
    color: rgb(254, 115, 0);
}

/deep/ .el-button--mini.is-circle {
    padding: 0px !important;
    border-color: white !important;
}
</style>

<style>
.el-popper {
    padding: 0px !important;
}
</style>
